package com.hmsg.health.controller.admin;
import com.hmsg.health.bean.domain.TaskTypeDo;
import com.hmsg.health.bean.enums.StatusCode;
import com.hmsg.health.bean.vo.TaskFirstReqVo;
import com.hmsg.health.bean.vo.TaskSecondReqVo;
import com.hmsg.health.dao.TaskTypeDao;
import com.hmsg.health.param.ResponseResult;
import com.hmsg.health.security.IgnoreSecurity;
import com.hmsg.health.security.OpenApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Api(value = "运营系统任务类型相关", tags = { "运营系统任务类型相关" })
@Slf4j
@Controller
@RequestMapping("/task")
public class AdminTaskTypeController {
    @Autowired
    private TaskTypeDao taskTypeDao;




    @GetMapping("/query")
    @ApiOperation(value="查询一级分类", notes="一级分类" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    public ResponseResult<List<TaskTypeDo>> queryFLei() {
        List<TaskTypeDo> taskTypes=taskTypeDao.findQuery();
        return new ResponseResult<List<TaskTypeDo>>(StatusCode.SYSTEM_SUCCESS, taskTypes);
    }

    @GetMapping("/queryClass")
    @ApiOperation(value="查询二级分类", notes="二级分类" ,httpMethod="GET")
    @ResponseBody
    @IgnoreSecurity
    @ApiImplicitParams({
            @ApiImplicitParam(name = "parentId", value = "1", required = true, example = "1", dataType = "int", paramType = "query")
    })
    public ResponseResult<List<TaskTypeDo>> queryTwoClass(@RequestParam(value = "parentId", required = true) Integer parentId) {
        List<TaskTypeDo> taskTypes=taskTypeDao.queryTwoClass(parentId);
        return new ResponseResult<List<TaskTypeDo>>(StatusCode.SYSTEM_SUCCESS, taskTypes);
    }

    @PostMapping("/addSecond")
    @ApiOperation(value="添加任务二级", notes="添加任务类型" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> addSecond( @ApiParam(name = "添加任务二级", value = "传入json格式", required = true)
                                                    @RequestBody @Validated TaskSecondReqVo vo) {
        TaskTypeDo taskTypeDo=new TaskTypeDo();
        taskTypeDo.setName(vo.getSecondName());
        taskTypeDo.setImage(vo.getImage());
        taskTypeDo.setParentId(vo.getParentId());
        taskTypeDo.setStatus(vo.getIsStatus());
        taskTypeDo.setTepOne(vo.getTepOne());
        taskTypeDo.setTepTwo(vo.getTepTwo());
        taskTypeDo.setSlogan(vo.getSlogan());
        taskTypeDao.save(taskTypeDo);
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }


    @PostMapping("/addFirst")
    @ApiOperation(value="添加任务一级", notes="添加任务类型" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> addFirstTasks( @ApiParam(name = "添加任务一级", value = "传入json格式", required = true)
                                         @RequestBody @Validated TaskFirstReqVo vo) {

            TaskTypeDo taskTypeDo=new TaskTypeDo();
            taskTypeDo.setName(vo.getTaskParentName());
            taskTypeDo.setIcon(vo.getIcon());
            taskTypeDao.save(taskTypeDo);
            return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);

    }

    @PostMapping("/editSecond")
    @ApiOperation(value="编辑任务二级", notes="编辑任务二级" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> editSecond( @ApiParam(name = "编辑任务二级", value = "传入json格式", required = true)
                                             @RequestBody @Validated TaskSecondReqVo vo) {
        TaskTypeDo taskTypeDo=new TaskTypeDo();
        taskTypeDo.setName(vo.getSecondName());
        taskTypeDo.setImage(vo.getImage());
        taskTypeDo.setParentId(vo.getParentId());
        taskTypeDo.setStatus(vo.getIsStatus());
        taskTypeDo.setTepOne(vo.getTepOne());
        taskTypeDo.setTepTwo(vo.getTepTwo());
        taskTypeDo.setId(vo.getTaskId());
        taskTypeDo.setSlogan(vo.getSlogan());
        taskTypeDao.update(taskTypeDo);
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }

    @PostMapping("/editFirst")
    @ApiOperation(value="编辑任务一级", notes="编辑任务一级" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> editFirst( @ApiParam(name = "编辑任务一级", value = "传入json格式", required = true)
                                              @RequestBody @Validated TaskFirstReqVo vo) {
        TaskTypeDo taskTypeDo=new TaskTypeDo();
        taskTypeDo.setName(vo.getTaskParentName());
        taskTypeDo.setIcon(vo.getIcon());
        taskTypeDo.setId(vo.getParentId());
        taskTypeDao.update(taskTypeDo);
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }


    @PostMapping("/deleteSecond")
    @ApiOperation(value="删除任务二级", notes="删除任务二级" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> deleteSecond( @ApiParam(name = "删除任务二级", value = "传入json格式", required = true)
                                             @RequestBody @Validated TaskSecondReqVo vo) {
        TaskTypeDo taskTypeDo=new TaskTypeDo();
        taskTypeDo.setId(vo.getTaskId());
        taskTypeDao.delete(taskTypeDo);
        return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
    }



    @PostMapping("/deleteFirst")
    @ApiOperation(value="删除任务一级", notes="删除任务一级" ,httpMethod="POST")
    @ResponseBody
    @IgnoreSecurity
    @OpenApi
    public ResponseResult<Object> deleteFirst( @ApiParam(name = "删除任务一级", value = "传入json格式", required = true)
                                                @RequestBody @Validated TaskSecondReqVo vo) {
        List<TaskTypeDo> taskTypes=taskTypeDao.queryTwoClass(vo.getParentId());
        if(taskTypes != null && taskTypes.size()>0){
            return ResponseResult.build(-1,"请先删除一级分类下面的所有二级分类！",null);

        }else{
            TaskTypeDo taskTypeDo=new TaskTypeDo();
            taskTypeDo.setId(vo.getParentId());
            taskTypeDao.delete(taskTypeDo);
            return new ResponseResult<Object>(StatusCode.SYSTEM_SUCCESS, null);
        }


    }



}
